main_num = int(input())
def get_numbers(x):
    m = []
    for s in str(x):
        m.append(s)
    return m
def prostota_chek(x):
    prostoe = True
    for i in range(2,x-1):
        if x%i == 0:
           prostoe = False
    return prostoe
def chek_poradok(x,massive):
    a = False
    if len(str(x))==2:
        if (str(x)[0] == massive[0] and str(x)[1] == massive[1]) or (str(x)[0] == massive[1] and str(x)[1] == massive[2]) or (str(x)[0] == massive[2] and str(x)[1] == massive[3]):
            a = True
    elif len(str(x))==3:
        if (str(x)[0] == massive[0] and str(x)[1] == massive[1] and str(x)[2]  == massive[2]) or (str(x)[0] == massive[1] and str(x)[1] == massive[2] and str(x)[2]  == massive[3]):
            a = True
    elif len(str(x))==4:
        if (str(x)[0] == massive[0] and str(x)[1] == massive[1] and str(x)[2] == massive[2] and str(x)[3] == massive[3]):
            a = True
    else:
        if str(x)[0] == massive[0] or str(x)[0] == massive[1] or str(x)[0] == massive[2] or str(x)[0] == massive[3]:
            a = True
        pass
    return a


def get_variacii(massive_num):
    variation = []
    if len(massive_num)==4:
        x = int(massive_num[0]) * 1000 + int(massive_num[1]) * 100 + int(massive_num[2]) * 10 + int(massive_num[3])
        for num in range(2,x+1):
            if num < 10:
                if chek_poradok(num,massive_num):
                    if prostota_chek(num):
                        variation.append(num)
            elif num < 100 and num >= 10:
                if chek_poradok(num,massive_num):
                    if prostota_chek(num):
                        variation.append(num)
            elif num < 1000 and num >= 100:
                if chek_poradok(num,massive_num):
                    if prostota_chek(num):
                        variation.append(num)
            elif num < 10000 and num >= 1000:
                if chek_poradok(num,massive_num):
                    if prostota_chek(num):
                        variation.append(num)
        if len(variation)!=0:
            return max(variation)
        else:
            return 0
    else:
        return 0
    return variation
print(  get_variacii(get_numbers(main_num))  )